package com.unittec.zk.provider.auth.controller;

import com.alibaba.fastjson.JSON;
import com.unittec.zk.provider.auth.entity.SysDeployFormEntity;
import com.unittec.zk.provider.auth.entity.SysFormEntity;
import com.unittec.zk.provider.auth.service.SysDeployFormService;
import com.unittec.zk.provider.auth.service.SysFormService;
import com.unittec.zk.provider.log.Log;
import com.unittec.zk.provider.utils.ValidationUtils;
import com.unittec.zk.sdk.auth.domain.SysDeployFormDto;
import com.unittec.zk.sdk.auth.domain.SysFormDto;
import com.unittec.zk.sdk.auth.request.*;
import com.unittec.zk.sdk.auth.response.*;
import com.unittec.zk.sdk.root.domain.GatewayRequest;
import com.unittec.zk.sdk.root.domain.Result;
import com.unittec.zk.sdk.root.enums.BizCode;
import com.unittec.zk.sdk.root.enums.BizCodeEnum;
import com.unittec.zk.sdk.root.enums.ErrorEnum;
import com.unittec.zk.sdk.root.internal.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "系统表单")
public class SysFormController {
    @Autowired
    Log log;

    @Autowired
    SysFormService sysFormService;

    @Autowired
    SysDeployFormService sysDeployFormService;

    /**
     * 查询流程表单列表
     */
    @RequestMapping(value = "/101671", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "查询流程表单列表", notes = "查询流程表单列表")
    public Result<Res101671> list(@RequestBody GatewayRequest<Req101671> req){
        log.debug(req, "list  {} ,param {}", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101671 res101671 = null;
        try {
            Req101671 content = req.getContent();
            ValidationUtils.validateEntityStr(content);
            res101671 = new Res101671();
            SysFormDto sysForm = content.getSysForm();
            SysFormEntity entity = new SysFormEntity();
            if(sysForm != null){
                BeanUtils.copyProperties(sysForm,entity);
            }
            List<SysFormEntity> list =  sysFormService.selectSysFormList(entity);
            List<SysFormDto> sysForms = new ArrayList<SysFormDto>();
            for(SysFormEntity ent : list){
                SysFormDto sysFrm = new SysFormDto();
                if(ent != null){
                    BeanUtils.copyProperties(ent,sysFrm);
                }
                sysForms.add(sysFrm);
            }
            res101671.setList(sysForms);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            if (e instanceof BusinessException) {
                return Result.error(req, e.getMessage());
            } else {
                return Result.error(req, ErrorEnum.QUERY_ERROR.getErrorMessage());
            }
        }
        log.debug(req, "list {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101671);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    /**
     * 导出流程表单列表
     */
    @RequestMapping(value = "/101672", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "导出流程表单列表", notes = "导出流程表单列表")
    public Result<Res101672> export(@RequestBody GatewayRequest<Req101672> req){
        log.debug(req, "export  {} ,param {}", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101672 res101672 = null;
        try {
            Req101672 content = req.getContent();
            ValidationUtils.validateEntityStr(content);
            res101672 = new Res101672();
            SysFormDto sysForm = content.getSysForm();
            SysFormEntity entity = new SysFormEntity();
            if(sysForm != null){
                BeanUtils.copyProperties(sysForm,entity);
            }
            List<SysFormEntity> list =  sysFormService.selectSysFormList(entity);
            List<SysFormDto> sysForms = new ArrayList<SysFormDto>();
            for(SysFormEntity ent : list){
                SysFormDto sysFrm = new SysFormDto();
                if(ent != null){
                    BeanUtils.copyProperties(ent,sysFrm);
                }
                sysForms.add(sysFrm);
            }
            res101672.setList(sysForms);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            if (e instanceof BusinessException) {
                return Result.error(req, e.getMessage());
            } else {
                return Result.error(req, ErrorEnum.QUERY_ERROR.getErrorMessage());
            }
        }
        log.debug(req, "export {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101672);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    /**
     * 获取流程表单详细信息
     */
    @RequestMapping(value = "/101673", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "获取流程表单详细信息", notes = "获取流程表单详细信息")
    public Result<Res101673> getInfo(@RequestBody GatewayRequest<Req101673> req){
        log.debug(req, "getInfo  {} ,param {}", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101673 res101673 = null;
        try {
            Req101673 content = req.getContent();
            ValidationUtils.validateEntityStr(content);
            res101673 = new Res101673();
            SysFormEntity entity = sysFormService.selectSysFormById(content.getId());
            SysFormDto sysForm = new SysFormDto();
            if(entity != null){
                BeanUtils.copyProperties(entity,sysForm);
            }
            res101673.setSysForm(sysForm);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            if (e instanceof BusinessException) {
                return Result.error(req, e.getMessage());
            } else {
                return Result.error(req, ErrorEnum.QUERY_ERROR.getErrorMessage());
            }
        }
        log.debug(req, "getInfo {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101673);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }


    /**
     * 新增流程表单
     */
    @RequestMapping(value = "/101674", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增流程表单", notes = "新增流程表单")
    public Result<Res101674> add(@RequestBody GatewayRequest<Req101674> req){
        log.debug(req, "add  {} ,param {}", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101674 res101674 = null;
        try {
            Req101674 content = req.getContent();
            ValidationUtils.validateEntityStr(content);
            res101674 = new Res101674();
            SysFormDto sysForm = content.getSysForm();
            SysFormEntity entity = new SysFormEntity();
            if(sysForm != null){
                BeanUtils.copyProperties(sysForm,entity);
            }
            int mark = sysFormService.insertSysForm(entity);
            res101674.setContent(mark==1?"表单导入成功":"表单导入失败");
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            if (e instanceof BusinessException) {
                return Result.error(req, e.getMessage());
            } else {
                return Result.error(req, ErrorEnum.QUERY_ERROR.getErrorMessage());
            }
        }
        log.debug(req, "add {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101674);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    /**
     * 修改流程表单
     */
    @RequestMapping(value = "/101675", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改流程表单", notes = "修改流程表单")
    public Result<Res101675> edit(@RequestBody GatewayRequest<Req101675> req){
        log.debug(req, "edit  {} ,param {}", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101675 res101675 = null;
        try {
            Req101675 content = req.getContent();
            ValidationUtils.validateEntityStr(content);
            res101675 = new Res101675();
            SysFormDto sysForm = content.getSysForm();
            SysFormEntity entity = new SysFormEntity();
            if(sysForm != null){
                BeanUtils.copyProperties(sysForm,entity);
            }
            int mark = sysFormService.updateSysForm(entity);
            res101675.setContent(mark==1?"表单修改成功":"表单修改失败");
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            if (e instanceof BusinessException) {
                return Result.error(req, e.getMessage());
            } else {
                return Result.error(req, ErrorEnum.QUERY_ERROR.getErrorMessage());
            }
        }
        log.debug(req, "edit {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101675);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    /**
     * 删除流程表单
     */
    @RequestMapping(value = "/101676", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除流程表单", notes = "删除流程表单")
    public Result<Res101676> remove(@RequestBody GatewayRequest<Req101676> req){
        log.debug(req, "remove  {} ,param {}", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101676 res101676 = null;
        try {
            Req101676 content = req.getContent();
            ValidationUtils.validateEntityStr(content);
            res101676 = new Res101676();
            int mark = sysFormService.deleteSysFormByIds(content.getIds());
            res101676.setContent(mark==1?"表单删除成功":"表单删除失败");
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            if (e instanceof BusinessException) {
                return Result.error(req, e.getMessage());
            } else {
                return Result.error(req, ErrorEnum.QUERY_ERROR.getErrorMessage());
            }
        }
        log.debug(req, "remove {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101676);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }


    /**
     * 挂载流程表单
     */
    @RequestMapping(value = "/101677", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "挂载流程表单", notes = "挂载流程表单")
    public Result<Res101677> addDeployForm(@RequestBody GatewayRequest<Req101677> req){
        log.debug(req, "addDeployForm  {} ,param {}", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101677 res101677 = null;
        try {
            Req101677 content = req.getContent();
            ValidationUtils.validateEntityStr(content);
            res101677 = new Res101677();
            SysFormDto sysForm = content.getSysForm();
            SysDeployFormEntity entity = new SysDeployFormEntity();
            if(sysForm != null){
                BeanUtils.copyProperties(sysForm,entity);
            }
            sysDeployFormService.insertSysDeployForm(entity);
            res101677.setContent("挂载表单成功");
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            if (e instanceof BusinessException) {
                return Result.error(req, e.getMessage());
            } else {
                return Result.error(req, ErrorEnum.QUERY_ERROR.getErrorMessage());
            }
        }
        log.debug(req, "addDeployForm {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101677);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }
}
